﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;

namespace IHISCETeim.Web.MachineSys.UserModel
{
    public partial class UWarrantyCardExList : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看, Permission.删除, Permission.新增, Permission.修改 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "UWarrantyCardExList";
            }
        }
        #region 属性 (查询条件)




        protected string IntoDateFrom
        {
            get
            {
                return GetRequestValue<string>("IntoDateFrom","");

            }
        }

        protected string IntoDateTo
        {
            get
            {
                return GetRequestValue<string>("IntoDateTo", "");

            }
        }
        protected string ShowAgentName
        {
            get
            {
                return GetRequestValue<string>("ShowAgentName", "");

            }
        }
        protected string SerialNumber
        {
            get
            {
                return GetRequestValue<string>("SerialNumber", "");

            }
        }


        protected bool IsVip
        {
            get
            {
                return GetRequestValue<bool>("IsVip", false);

            }
        }

       

        protected override string orderField
        {
            get
            {
                return GetRequestValue<string>("orderfield", "UWCID");
            }
        }

        protected override string orderDirection
        {
            get
            {
                return GetRequestValue<string>("orderDirection", "asc");
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            DataBind();
        }
        protected string GetSQLWhere()
        {

            string wheresql = "";
            string sqlcommonFiled = "";
            if (!string.IsNullOrEmpty(SerialNumber))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("SerialNumber", QueryOperatorsEnum.包含, SerialNumber);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }           



            if (IntoDateFrom != "")
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("IntoDate", QueryOperatorsEnum.范围, IntoDateFrom + "|" + IntoDateTo);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
            if (IsVip)
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("isvip", QueryOperatorsEnum.等于, "1");
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }

            if (!string.IsNullOrEmpty(ShowAgentName))
            {
             
                string sqlAgentName=  "'" + ShowAgentName + "'";
                sqlAgentName = sqlAgentName.Replace(",", "','");
                sqlcommonFiled = string.Format("AgentName in({0})", sqlAgentName);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }    

            return wheresql;
        }
        protected void DataBind()
        {
            this.rpMenu.DataSource = UWarrantyCardController.GetPagination(GetSQLWhere(), pageNum, numPerPage, SQLSort, ref TotalCount)[0];
            this.rpMenu.DataBind();
        }

        #region
        protected string GetAgentList()
        {
            DataTable agentmaclist = UWarrantyCardController.GetAgentMacNum();

            string litemp = "<li ><a tname=\"{7}\" tvalue=\"{1}\" title=\"全称:[{2}].公司地址:[{3}]\"  style=\"{4}\" {5}>{0}(<b style='color:red;'>{6}</b>)</a></li>";
            string listr = "";
            //DataTable dt= SBOController.GetAgentList();
            DataTable dt = StringHelper.GetDTToPinYin(SBOController.GetAgentList(), "AliasName", "PYAliasName", " Asc");
            foreach (DataRow dr in dt.Rows)
            {
                //string x = ShowAgentName == (string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"].ToString() : dr["AliasName"].ToString()) ? "color:red;font-weight:bold;" : "";
                int pot= ShowAgentName.IndexOf(string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"].ToString() : dr["AliasName"].ToString());
                string x = pot >= 0 ? "color:red;font-weight:bold;" : "";
                string check = pot >= 0 ? "checked='true'" : "";
                DataRow[] drs = agentmaclist.Select("agentname='" + (string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"] : dr["AliasName"]) + "'");
                string agstock = "0";
                if (drs.Count() > 0)
                {
                    agstock = drs[0]["macnum"].ToString();
                }
                string agentname = (string.IsNullOrEmpty(dr["AliasName"].ToString()) ? dr["CardName"].ToString() : dr["AliasName"].ToString());
                listr += string.Format(litemp, dr["PYAliasName"] + agentname, dr["CardCode"], dr["CardName"], dr["Address"], x, check, agstock, agentname);
            }
            return listr;
        }

        protected string GetSMSTempleteList()
        {
            string litemp = "<option value=\"{0}\" {1}>{0}</option>";
            string listr = "";
            List<USMSTempleteInfo> dt = USMSTempleteController.GetAll();
            foreach (USMSTempleteInfo dr in dt)
            {

                listr += string.Format(litemp, dr.Tkey, dr.Tkey == "日常使用注意事项"?"selected":"");
            }
            return listr;
        }
        #endregion
    }
}